/**********************************************************************************************************
** 驱动文件：	ups-MP1100.c
** 驱动类型：	ups
** 设备名称：	柏克UPS
** 设备型号：	MP1100
** 设备品牌：	柏克
** 设备协议：	Modbus-RTU
** 驱动描述：	适用于柏克MP1100系列单进单出工频UPS电源，通讯协议为Modbus-RTU。
** 生成日期：	2018-10-26 22:53:07
**********************************************************************************************************/
#include "Includes.h"


/**********************************************************************************************************
* 定义设备数据点变量结构
**********************************************************************************************************/
typedef struct{
	Var inputVoltage;							//输入电压
	Var inputFreq;								//输入频率
	Var bypassVoltage;							//旁路电压
	Var bypassFreq;								//旁路频率
	Var outputVoltage;							//输出电压
	Var outputFreq;								//输出频率
	Var loadCapacity;							//负载容量
	Var batteryVoltage;							//电池电压
	Var batteryChargeCurrent;					//电池充电电流
	Var temperature;							//温度
	Var DCinputVoltage;							//DC输入电压
	Var rectifierInputStatus;					//整流输入状态
	Var rectifierInputPhaseSequence;			//整流输入相序状态
	Var bypassInputStatus;						//旁路输入状态
	Var bypassInputPhaseSequence;				//旁路输入相序状态
	Var inverterStatus;							//逆变器状态
	Var outputStatus;							//输出状态
	Var overTemperature;						//过温告警
	Var forcedSart;								//强制启动告警
	Var workingState;							//机器工作状态
	Var airSwitchAndFuseStatus;					//空开和保险丝状态
	Var workingMode;							//当前工作模式
	Var rectifierChargerStatus;					//整流器/充电器状态
	Var batteryStatus;							//电池状态
	Var loadStatus;								//负载状态
	Var batteryTestStatus;						//电池测试状态
	Var DCinputStatus;							//DC状态报警
	Var systemFailure;							//系统状态
}Data_Struct;									//定义变量结构


/**********************************************************************************************************
* 设备数据点变量属性声明
**********************************************************************************************************/
const static Var_Property_Def var_attr[] = {
	{
		FLOAT,
		0,
		READONLY,
		"inputVoltage",
		"输入电压",
		"V",
		"整流输入电压",
		0,
		0,
		"整流输入电压过高",
		"整流输入电压过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"inputFreq",
		"输入频率",
		"Hz",
		"整流输入频率",
		0,
		0,
		"整流输入频率过高",
		"整流输入频率过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"bypassVoltage",
		"旁路电压",
		"V",
		"旁路输入电压",
		0,
		0,
		"旁路输入电压过高",
		"旁路输入电压过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"bypassFreq",
		"旁路频率",
		"Hz",
		"旁路输入频率",
		0,
		0,
		"旁路输入频率过高",
		"旁路输入频率过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"outputVoltage",
		"输出电压",
		"V",
		"逆变输出电压",
		0,
		0,
		"逆变输出电压过高",
		"逆变输出电压过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"outputFreq",
		"输出频率",
		"Hz",
		"逆变输出频率",
		0,
		0,
		"逆变输出频率过高",
		"逆变输出频率过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"loadCapacity",
		"负载容量",
		"%",
		"负载容量",
		0,
		0,
		"负载容量过高",
		"负载容量过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"batteryVoltage",
		"电池电压",
		"V",
		"电池电压",
		0,
		0,
		"电池电压过高",
		"电池电压过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"batteryChargeCurrent",
		"电池充电电流",
		"A",
		"电池充电电流",
		0,
		0,
		"电池充电电流过高",
		"电池充电电流过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"temperature",
		"温度",
		"℃",
		"温度",
		0,
		0,
		"温度过高",
		"温度过低",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"DCinputVoltage",
		"DC输入电压",
		"V",
		"DC输入电压",
		0,
		0,
		"DC输入电压过高",
		"DC输入电压过低",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"rectifierInputStatus",
		"整流输入状态",
		"",
		"整流输入状态：0-正常；1-异常",
		0,
		0,
		"整流输入异常",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"rectifierInputPhaseSequence",
		"整流输入相序状态",
		"",
		"整流输入相序状态：0-正常；1-异常",
		0,
		0,
		"整流输入相序异常",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"bypassInputStatus",
		"旁路输入状态",
		"",
		"旁路输入状态：0-正常；1-异常",
		0,
		0,
		"旁路输入异常",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"bypassInputPhaseSequence",
		"旁路输入相序状态",
		"",
		"旁路输入相序状态：0-正常；1-异常",
		0,
		0,
		"旁路输入相序异常",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"inverterStatus",
		"逆变器状态",
		"",
		"逆变器状态：0-关闭；1-运行",
		0,
		0,
		"逆变器关闭",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"outputStatus",
		"输出状态",
		"",
		"输出状态：0-逆变状态；1-旁路输出",
		0,
		0,
		"旁路输出",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"overTemperature",
		"过温告警",
		"",
		"UPS过温告警：0-正常；1-过温",
		0,
		0,
		"UPS过温",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"forcedSart",
		"强制启动告警",
		"",
		"强制启动告警：0-正常；1-强制启动",
		0,
		0,
		"强制启动",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"workingState",
		"机器工作状态",
		"",
		"机器工作状态：0-关机；1-运行",
		0,
		0,
		"关机",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"airSwitchAndFuseStatus",
		"空开和保险丝状态",
		"",
		"空开和保险丝状态：0-正常；1-异常",
		0,
		0,
		"空开和保险丝异常",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"workingMode",
		"当前工作模式",
		"",
		"机器当前工作模式：0-UPS；1-ECO；2-EPS",
		0,
		0,
		"",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"rectifierChargerStatus",
		"整流器/充电器状态",
		"",
		"整流器/充电器状态：0-关闭；1-运行",
		0,
		0,
		"整流器/充电器关闭",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"batteryStatus",
		"电池状态",
		"",
		"电池状态：0-正常；1-低压",
		0,
		0,
		"电池低压",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"loadStatus",
		"负载状态",
		"",
		"负载状态：0-正常；1-过载",
		0,
		0,
		"负载过载",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"batteryTestStatus",
		"电池测试状态",
		"",
		"电池测试状态：0-正常；1-测试中",
		0,
		0,
		"电池测试中",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"DCinputStatus",
		"DC状态报警",
		"",
		"DC状态：0-正常；1-异常",
		0,
		0,
		"DC状态异常",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"systemFailure",
		"系统状态",
		"",
		"系统状态：0-正常；1-故障",
		0,
		0,
		"系统故障",
		"",
		0.000,
		0.000
	}
};


/**********************************************************************************************************
* 函数名称： static void cmd_fun(short index, int addr, void *data, char *cmd, short *len)
* 函数说明： 生成获取传感器数据指令
* 输入参数： 指令索引，传感器地址，变量结构体指针,以及指令缓存区指针,指令长度
* 返回参数： 无
**********************************************************************************************************/
static void cmd_fun(short index, int addr, void *data, char *cmd, short *len)
{
	switch(index)
	{
		case 0:
			cmd[0] = addr;
			cmd[1] = 0x03;
			cmd[2] = 0x00;								
			cmd[3] = 0xC8;
			cmd[4] = 0x00;	
			cmd[5] = 0x28;
			*len = CRC16_Padding(cmd, 6);
			break;
		default:
			break;
	}
}


/**********************************************************************************************************
* 函数名称： static int parse_fun(short index, int addr, void *data, char *buf, short len)
* 函数说明： 解析传感器响应的数据，并写入数据库
* 输入参数： 解析索引,传感器地址，变量结构体指针，以及响应数据缓存取指针，响应数据长度
* 返回参数： 返回解析结果，返回0成功，返回1失败
**********************************************************************************************************/
static int parse_fun(short index, int addr, void *data, char *buf, short len)
{
	union{
		short 			dat16;
      	unsigned char  	dat8[2];
    }DAT16_8;						   //数据类型转换	
	Data_Struct	*d = (Data_Struct*)data;

	if(buf!=NULL && buf[0]==addr && len>0 )
	{	
		if(CRC16_Check(buf,len))
		{	
			DAT16_8.dat8[1] = buf[3];
			DAT16_8.dat8[0] = buf[4];
			d->inputVoltage.value.fnum = DAT16_8.dat16*0.1;

			DAT16_8.dat8[1] = buf[9];
			DAT16_8.dat8[0] = buf[10];
			d->inputFreq.value.fnum = DAT16_8.dat16*0.1;
			
			DAT16_8.dat8[1] = buf[11];
			DAT16_8.dat8[0] = buf[12];
			d->bypassVoltage.value.fnum = DAT16_8.dat16*0.1;	
			
			DAT16_8.dat8[1] = buf[17];
			DAT16_8.dat8[0] = buf[18];
			d->bypassFreq.value.fnum = DAT16_8.dat16*0.1;
			
			DAT16_8.dat8[1] = buf[19];
			DAT16_8.dat8[0] = buf[20];
			d->outputVoltage.value.fnum = DAT16_8.dat16*0.1;	

			DAT16_8.dat8[1] = buf[25];
			DAT16_8.dat8[0] = buf[26];
			d->outputFreq.value.fnum = DAT16_8.dat16*0.1;
			
			DAT16_8.dat8[1] = buf[27];
			DAT16_8.dat8[0] = buf[28];
			d->loadCapacity.value.fnum = DAT16_8.dat16*0.1;	
			
			DAT16_8.dat8[1] = buf[45];
			DAT16_8.dat8[0] = buf[46];
			d->batteryVoltage.value.fnum = DAT16_8.dat16*0.1;			
			DAT16_8.dat8[1] = buf[47];
			DAT16_8.dat8[0] = buf[48];
			d->batteryChargeCurrent.value.fnum = DAT16_8.dat16*0.1;	
			DAT16_8.dat8[1] = buf[49];
			DAT16_8.dat8[0] = buf[50];
			d->temperature.value.fnum = DAT16_8.dat16*0.1;			
			DAT16_8.dat8[1] = buf[53];
			DAT16_8.dat8[0] = buf[54];
			d->DCinputVoltage.value.fnum = DAT16_8.dat16*0.1;	

			DAT16_8.dat8[1] = buf[73];
			DAT16_8.dat8[0] = buf[74];
			d->rectifierInputStatus.value.str				= DAT16_8.dat16&0x0001 ? "异常":"正常";
			Set_Var_Status(&d->rectifierInputStatus,		DAT16_8.dat16&0x0001?Alarm:Normal, NULL);
			d->rectifierInputPhaseSequence.value.str		= DAT16_8.dat16&0x0002 ? "异常":"正常";
			Set_Var_Status(&d->rectifierInputPhaseSequence,	DAT16_8.dat16&0x0002?Alarm:Normal, NULL);
			d-> bypassInputStatus.value.str					= DAT16_8.dat16&0x0004 ? "异常":"正常";
			Set_Var_Status(&d-> bypassInputStatus, 			DAT16_8.dat16&0x0004?Alarm:Normal, NULL);
			d->bypassInputPhaseSequence.value.str			= DAT16_8.dat16&0x0008 ? "异常":"正常";
			Set_Var_Status(&d->bypassInputPhaseSequence, 	DAT16_8.dat16&0x0008?Alarm:Normal, NULL);
			d->inverterStatus.value.str						= DAT16_8.dat16&0x0010 ? "运行":"关闭";
			Set_Var_Status(&d->inverterStatus, 				DAT16_8.dat16&0x0010?Normal:Alarm, NULL);	
			d->outputStatus.value.str						= DAT16_8.dat16&0x0020 ? "旁路输出":"逆变输出";
			Set_Var_Status(&d->outputStatus, 				DAT16_8.dat16&0x0020?Alarm:Normal, NULL);			
			d->overTemperature.value.str					= DAT16_8.dat16&0x0040 ? "过温":"正常";
			Set_Var_Status(&d->overTemperature, 			DAT16_8.dat16&0x0040?Alarm:Normal, NULL);
			d->forcedSart.value.str							= DAT16_8.dat16&0x0080 ? "强制重启":"正常";
			Set_Var_Status(&d->forcedSart, 					DAT16_8.dat16&0x0080?Alarm:Normal, NULL);
			d->workingState.value.str						= DAT16_8.dat16&0x0100 ? "运行":"关机";
			Set_Var_Status(&d->workingState, 				DAT16_8.dat16&0x0100?Normal:Alarm, NULL);
			d->airSwitchAndFuseStatus.value.str				= DAT16_8.dat16&0x0200 ? "异常":"正常";
			Set_Var_Status(&d->airSwitchAndFuseStatus, 		DAT16_8.dat16&0x0200?Alarm:Normal, NULL);
			
			if((DAT16_8.dat16&0x0C00)==0x0000){
				d->workingMode.value.str = "UPS模式";
			}
			else if((DAT16_8.dat16&0x0C00)==0x0400){
				d->workingMode.value.str = "ECO模式";
			}
			else if((DAT16_8.dat16&0x0C00)==0x0800){
				d->workingMode.value.str = "EPS模式";
			}
			else if((DAT16_8.dat16&0x0C00)==0x0C00){
				d->workingMode.value.str = "UPS模式";
			}			

			d->rectifierChargerStatus.value.str				= DAT16_8.dat16&0x1000 ? "运行":"关闭";
			Set_Var_Status(&d->rectifierChargerStatus, 		DAT16_8.dat16&0x1000?Normal:Alarm, NULL);			
			d->batteryStatus.value.str						= DAT16_8.dat16&0x2000 ? "低压":"正常";
			Set_Var_Status(&d->batteryStatus, 				DAT16_8.dat16&0x2000?Alarm:Normal, NULL);
			d->loadStatus.value.str							= DAT16_8.dat16&0x4000 ? "过载":"正常";
			Set_Var_Status(&d->loadStatus, 					DAT16_8.dat16&0x4000?Alarm:Normal, NULL);
			d->batteryTestStatus.value.str					= DAT16_8.dat16&0x8000 ? "测试中":"正常";
			Set_Var_Status(&d->batteryTestStatus, 			DAT16_8.dat16&0x8000?Alarm:Normal, NULL);			

			DAT16_8.dat8[1] = buf[75];
			DAT16_8.dat8[0] = buf[76];			
			d->DCinputStatus.value.str						= DAT16_8.dat16&0x0001 ? "异常":"正常";
			Set_Var_Status(&d->DCinputStatus, 				DAT16_8.dat16&0x0001?Alarm:Normal, NULL);
			d->systemFailure.value.str						= DAT16_8.dat16&0x0002 ? "故障":"正常";
			Set_Var_Status(&d->systemFailure, 				DAT16_8.dat16&0x0002?Alarm:Normal, NULL);				
			
			return 0;		
		}
	}
	return 1;
}


/**********************************************************************************************************
* 函数名称： void ups_MP1100_Registration(void)
* 函数说明： 柏克UPS驱动注册
* 输入参数： 无
* 返回参数： 无
**********************************************************************************************************/
void ups_MP1100_Registration(void)
{
	DeviceDriverRegistration(
		"ups",													//设备类型
		"柏克UPS",												//设备名称（导航栏默认显示名称）
		"柏克",													//设备品牌
		"MP1100",												//设备型号
		"适用于柏克MP1100系列单进单出工频UPS电源，通讯协议为Modbus-RTU。",//驱动描述
		var_attr,												//变量属性声明
		sizeof(Data_Struct),									//变量结构体空间大小
		cmd_fun,												//发送指令生成函数
		parse_fun,												//数据解析函数
		500														//设备响应超时时间
	);
}

